package com.rabbit2020.web;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.ExchangeBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 作者: LCG
 * 日期: 2020/2/26 00:29
 * 描述:
 */

@RestController
public class TestHongBao {

    @Autowired
    private AmqpAdmin amqpAdmin;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 发红包通知消息消息
     * @return
     */
    @RequestMapping("/sendMessage")
    public String sendMessage(@RequestBody Map<String,String> map){

        //发送消息
        rabbitTemplate.convertAndSend(map.get("exchange"),null,map.get("tongzhi"));

        return "ok";
    }

    /**
     * 获取队列
     * @return
     */
    @RequestMapping("getExchange")
    public String getExchange(@RequestBody Map<String,String> map){

        String exchange=map.get("exchange");
        amqpAdmin.declareExchange(ExchangeBuilder.fanoutExchange(exchange).durable(false).autoDelete().build());

        return exchange;
    }

}
